<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/23
 * Time: 15:19
 */

namespace app\logic\index\article;


use app\common\lib\Ai\OpenApi;
use app\common\lib\Common;
use app\common\lib\Guzzle;
use app\common\lib\RedisQueue;
use app\exception\IndexException;
use app\logic\BaseLogic;

class ArticleLogic extends BaseLogic
{
    protected $model;

    protected static $instance;

    public function __construct()
    {
        parent::__construct();
        $this->model = \app\model\user\UserPromotionArticleModel::getInstance();
    }

    public static function getInstance()
    {
//        if(is_object(self::$instance)){
//            return self::$instance;
//        }


        return new self();
    }

    public function submitData($param)
    {
        if(Common::setnx('submitData_'.request()->curUserId)==false){
            throw new IndexException(4020,'请求频繁');
        }

        if(mb_strlen($param['content'])<300 || mb_strlen($param['content'])>6000 ){
            throw new IndexException(4020,'内容文字要在300-6000之间');
        }

        //用户ai分段
        $parts = Common::aiContentPartsToUser($param['content']);

        //分段分析和字幕
        list($parts,$subTitles) = partsAnalyze($parts);

        $insert = [
            'user_sn'=>request()->curUserId,
            'content'=>$param['content'],
            'modelid'=>$param['modelid']??0,
            'templateid'=>$param['templateid']??0,
            'musicid'=>$param['musicid']??0,
            'timbreid'=>$param['timbreid']??0,
            'music_volume'=>$param['music_volume']??5,
            'timbre_volume'=>$param['timbre_volume']??5,
            'speak_speed'=>$param['speak_speed']??0,
            'fontid'=>$param['fontid']??0,
            'video_rate'=>$param['video_rate']??'3:2',
            'part_content'=>json_encode(array_values($parts),256),
            'subtitle'=>json_encode($subTitles,256),
        ];
        $id = $this->model->insertGetId($insert);

        $count = count($parts);
        $totoalPonint = round($count*config('param.image_consume_point',1));
        return ['id'=>$id,'count'=>$count,'total_point'=>$totoalPonint,'parts'=>$parts];
    }

    public function submitDataQueue($param)
    {
        if(Common::setnx('submitData_Queue_'.request()->curUserId)==false){
            throw new IndexException(4020,'请求频繁');
        }

        if(mb_strlen($param['content'])<300 || mb_strlen($param['content'])>6000 ){
            throw new IndexException(4020,'内容文字要在300-6000之间');
        }

        //用户ai分段
//        $parts = Common::aiContentPartsToUser($param['content']);
//
//        //分段分析和字幕
//        list($parts,$subTitles) = partsAnalyze($parts);

        $insert = [
            'user_sn'=>request()->curUserId,
            'content'=>$param['content'],
            'modelid'=>$param['modelid']??0,
            'templateid'=>$param['templateid']??0,
            'musicid'=>$param['musicid']??0,
            'timbreid'=>$param['timbreid']??0,
            'music_volume'=>$param['music_volume']??5,
            'timbre_volume'=>$param['timbre_volume']??10,
            'speak_speed'=>$param['speak_speed']??10,
            'fontid'=>$param['fontid']??0,
            'video_rate'=>$param['video_rate']??'3:2',
        ];
        $id = $this->model->insertGetId($insert);

        //生成唯一的请求id
        $requestId = Common::getRequestId();

        // 队列名
        $queue = 'asyncArticlePart';
        // 数据，可以直接传数组，无需序列化
        $data = ['requestid'=>$requestId,'id'=>$id];
        // 投递消息
        RedisQueue::getInstance()->sendQueue($queue, $data);

        return ['requestid'=>$requestId,'id'=>$id];
    }

    public function getAricleAnalyzeResult($param)
    {
        $article = $this->model->find($param['id']);
        if($article==false){
            throw new IndexException(4020,'文章内容不存在');
        }

        $count = 0;
        $parts = [];
        if($article['status']==1 && !empty($article['part_content'])){
            $parts = json_decode($article['part_content'], true);
            $count = count($parts);
        }

        $totoalPonint = round($count*config('param.image_consume_point',1));
        $message = !empty($article['message']) ? $article['message'] : '内容分段中...';

        return [
            'id'          => $article['id'], 'count' => $count, 'status' => $article['status'],
            'total_point' => $totoalPonint, 'message' => $message, 'parts' => $parts
        ];
    }
}